/*
 * Copyright 2012 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.expenses.client.activities.ed;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.googlecode.mgwt.expenses.client.misc.I18N;

public class ExpenseDetailViewDesktopGwtImpl extends Composite implements ExpenseDetailView {

	private static ExpenseDetailViewDesktopGwtImplUiBinder uiBinder = GWT.create(ExpenseDetailViewDesktopGwtImplUiBinder.class);

	interface ExpenseDetailViewDesktopGwtImplUiBinder extends UiBinder<Widget, ExpenseDetailViewDesktopGwtImpl> {
	}

	@UiField
	TextArea descriptionBox;

	@UiField
	TextBox amountBox;

	@UiField
	DateBox dateBox;

	@UiField
	Button editButton;

	@UiField
	TextBox titleText;

	@UiField
	Widget formContainer;

	@UiField
	Image image;

	private Presenter presenter;

	public ExpenseDetailViewDesktopGwtImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		dateBox.setFormat(new DateBox.DefaultFormat(I18N.DATE_FORMAT));
	}

	@Override
	public HasValue<String> getDescription() {
		return descriptionBox;
	}

	@Override
	public HasValue<String> getAmount() {
		return amountBox;
	}

	@Override
	public HasValue<Date> getDate() {
		return dateBox;
	}

	@Override
	public HasText getLeftButton() {
		return new HasText() {

			@Override
			public String getText() {
				return null;
			}

			@Override
			public void setText(String text) {

			}
		};
	}

	@Override
	public HasText getEditButton() {
		return editButton;
	}

	@Override
	public HasText getTitleText() {
		return titleText;
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		titleText.setReadOnly(readOnly);
		amountBox.setReadOnly(readOnly);
		dateBox.setEnabled(!readOnly);
		descriptionBox.setEnabled(!readOnly);

	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;

	}

	@Override
	public void setLoading(boolean loading) {
		formContainer.setVisible(!loading);

	}

	@Override
	public void showError(String string) {
		new MyDialog(string).show();
	}

	@Override
	public void showCameraButton(boolean show) {

	}

	@Override
	public void setImageUrl(String data) {
		image.setUrl(data);

	}

	@Override
	public void showFoto(boolean show) {
		image.setVisible(show);

	}

	private static class MyDialog extends DialogBox {

		public MyDialog(String txt) {
			// Set the dialog box's caption.
			setText(txt);

			// DialogBox is a SimplePanel, so you have to set its widget
			// property to
			// whatever you want its contents to be.
			Button ok = new Button("OK");
			ok.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					MyDialog.this.hide();

				}
			});

			setWidget(ok);
		}
	}

	@UiHandler("editButton")
	public void onEditButtonSelected(ClickEvent event) {
		if (presenter != null) {
			presenter.onEditButtonPressed();
		}
	}

}
